/**
 * Write By HQ_punan 2012.01.22
 * This Class is use for read xml use dom
 */


package com.huaqin.punan;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;

public class ReadXMLDom {
	public static final String TAG = "ReadXMLDom";
	public static boolean debug = true;
	public static final String FILE_PATH = MainActivity.FILE_PATH;
	public static final String FILE_NAME = MainActivity.FILE_NAME;
	
	static String[] ReadXMLFiles(){
		
		String[] salesinfo = new String[3];
		
		DocumentBuilderFactory docBuilderFactory = null;
		DocumentBuilder docBuilder = null;
		Document doc = null;
		
		if(debug) Log.d(TAG, "Read XML");
		
		try{
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docBuilderFactory.newDocumentBuilder();
			
			File file = new File(FILE_PATH + FILE_NAME);
			InputStream is = new FileInputStream(file);
			
			doc = docBuilder.parse(is);
			Element root = doc.getDocumentElement();
			
			NodeList items = root.getElementsByTagName("ngm");
			if(debug)Log.d(TAG, "items.getLength() = "+items.getLength());
			
			for(int i = 0; i < items.getLength(); i++){
				
				Element parameter = (Element)items.item(i);
				NodeList params = parameter.getChildNodes();
				if(debug)Log.d(TAG, "params.getLength() = "+params.getLength());
				
				for(int j = 0; j < params.getLength(); j++){
					
					Node param = (Node)params.item(j);
					if("send_flag".equals(param.getNodeName())){
						salesinfo[0] = param.getFirstChild().getNodeValue();
					}
					if("out_going".equals(param.getNodeName())){
						salesinfo[1] = param.getFirstChild().getNodeValue();
					}
					if("in_coming".equals(param.getNodeName())){
						salesinfo[2] = param.getFirstChild().getNodeValue();
					}
					
				}
			}
		}catch(IOException e){
			e.printStackTrace();
			
		}catch(SAXException e){
			e.printStackTrace();
			
		}catch(ParserConfigurationException e){
			e.printStackTrace();
		}finally{
			doc = null;
			docBuilder = null;
			docBuilderFactory = null;
		}
	return salesinfo;
	
	}
	
}
